//
//  LabelWaterLayout.m
//  网易上面的滑动栏
//
//  Created by mafeng on 16/8/3.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "LabelWaterLayout.h"
#import "LabelWaterLayout.h"
#import "MyCollectionViewCell.h"
@interface LabelWaterLayout ()
@property (nonatomic, strong)NSMutableArray<UICollectionViewLayoutAttributes *> *attributessArray;
@property (nonatomic, assign) CGFloat contentWidth;
@end
@implementation LabelWaterLayout

- (instancetype)init {
    self = [super init];
    if (self) {
        _attributessArray = [NSMutableArray array];
        
    }
    return self;
}
  // 要关联到sb的话, 要重写这个方法
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        _attributessArray = [NSMutableArray array];
    }
    return self;
}

- (void)prepareLayout {
    // 刷新描述布局数组
    self.contentWidth = 0;
    if (_attributessArray.count != 0) {
        [_attributessArray removeAllObjects];
    }
    // 添加描述布局数组
    CGFloat allWidth = 0;
    for (int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];
        // 每一个Item创建一个Attributes
        UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index];
        
        CGFloat x = allWidth;
        CGFloat y = 0;
        CGFloat height = 40;
        CGFloat width = [self.delegate collectionView:self.collectionView layout:self height:height widthForItemAtIndexPath:index];
        NSLog(@"---------%f", width);
        allWidth+=width;
        
        layoutAttributes.frame = CGRectMake(x, y, width, height);
        _contentWidth = allWidth;
        [_attributessArray addObject:layoutAttributes];
    }
   
}
 // 返回每个Item对应的布局属性
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return _attributessArray;
}
// 负责collectionView的移动范围
- (CGSize)collectionViewContentSize {
    return CGSizeMake(_contentWidth, 0);
}


@end
